//
//  ScViewController.swift
//  weibo03
//
//  Created by mac on 16/9/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class ScViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //使用kvc方式给oc中的类设置只读属性
        let scTaBar = ScTabBar()
        //4设置代理对象
        scTaBar.scDelegate = self
        
        //03使用weakself避免循环引用
        scTaBar.composeButtonClosure = {[weak self] in
            print("我是闭包调用")
        }
        setValue(scTaBar, forKey: "tabBar")
        
        
        //添加子视图控制器
        addChildViewController(childController: ScHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: ScMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: ScDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: ScProfileViewController(), title: "我的", imageName: "tabbar_profile")

        
    }
    //利用重载添加子视图控制器
    func addChildViewController(childController: UIViewController, title: String , imageName: String ) {
        
        //设置 tablebaritem的图片和文字
        childController.tabBarItem.image = UIImage(named: imageName)
        
        //修改图片的渲染模式(第二种设置颜色的方式)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置文字选中的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        //  设置文字大小
       // childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
        
        childController.tabBarItem.title = title
        childController.title = title
        
        //创建导航视图控制器
        let nav = ScNavController(rootViewController: childController)
        //添加子视图控制器
        addChildViewController(nav)
        
    }

    
}
// 5 使用extension可以实现代理方法
extension ScViewController: SCTabBarDelegate {
    
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
    
}


